package com.dykj.fireFighting.web.base.analysis.controller;

import cn.hutool.core.date.DateTime;
import com.dykj.fireFighting.api.base.analysis.entity.DevHisElectricalDto;
import com.dykj.fireFighting.api.base.analysis.entity.DeviceRes;
import com.dykj.fireFighting.api.base.analysis.entity.EarlyWarningResult;
import com.dykj.fireFighting.api.base.analysis.entity.TongHuanBiRes;
import com.dykj.fireFighting.api.base.analysis.service.IDeviceAnalysisService;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.entity.EsDevHisWater;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.service.EsDevHisWaterService;
import com.dykj.fireFighting.api.elasticsearch.devHisZc.entity.EsDevHisZc;
import com.dykj.fireFighting.api.elasticsearch.devHisZc.service.EsDevHisZcService;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.DateUtil;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/*
 * 设备分析
 */
@Slf4j
@Api(value = "设备分析", tags = {"设备分析"})
@RestController
@RequestMapping("/analysis/device")
public class DeviceAnalysisController {
    @DubboReference
    private EsDevHisZcService esDevHisZcService;
    @DubboReference
    private EsDevHisWaterService esDevHisWaterService;
    @DubboReference
    private IDeviceAnalysisService ideviceAnalysisService;
    @DubboReference
    private EsDevHisService esDevHisService;

    @ApiOperation(value = "电气火灾分析", notes = "电气火灾分析")
    @PostMapping("/electrical/list")
    public CommonResult<PageVo<EsDevHisZc>> treeGridElectrical(@RequestBody DevHisElectricalDto devHisElectricalDto) {
        Integer enterPriseType = ShiroUtil.getUser().getEnterpriseType();
        String centerId ="";
        String enterpriseId = "";
        if (enterPriseType == 0){
            enterpriseId = ShiroUtil.getUser().getEnterpriseId().toString();
            devHisElectricalDto.setUnitId(enterpriseId);
        }else if(enterPriseType == 2){
            centerId = ShiroUtil.getUser().getEnterpriseId().toString();
        }else {
            return CommonResult.failed();
        }
        if (null == devHisElectricalDto.getEndDate()){
            devHisElectricalDto.setEndDate(DateUtil.getNowDateStr());
        }
        if (null == devHisElectricalDto.getBeginDate()) {
            DateTime dt = cn.hutool.core.date.DateUtil.lastMonth();
            String s = dt.toDateStr();
            devHisElectricalDto.setBeginDate(s);
        }
        PageVo<EsDevHisZc> page = esDevHisZcService.searchByCenterId(centerId,devHisElectricalDto.getUnitId() , devHisElectricalDto.getName(),(int) devHisElectricalDto.getPage(), (int) devHisElectricalDto.getLimit(),
                devHisElectricalDto.getBeginDate(),devHisElectricalDto.getEndDate());
        return CommonResult.success(page);
    }

    @ApiOperation(value = "消防水分析", notes = "消防水分析")
    @PostMapping("/water/list")
    public CommonResult<PageVo<EsDevHisWater>> treeGridWater(@RequestBody DevHisElectricalDto devHisElectricalDto) {
        Integer enterPriseType = ShiroUtil.getUser().getEnterpriseType();
        String centerId ="";
        String enterpriseId = "";
        if (enterPriseType == 0){
            enterpriseId = ShiroUtil.getUser().getEnterpriseId().toString();
            devHisElectricalDto.setUnitId(enterpriseId);
        }else if(enterPriseType == 2){
            centerId = ShiroUtil.getUser().getEnterpriseId().toString();
        }else {
            return CommonResult.failed();
        }
        if (null == devHisElectricalDto.getEndDate()){
            devHisElectricalDto.setEndDate(DateUtil.getNowDateStr());
        }
        if (null == devHisElectricalDto.getBeginDate()) {
            DateTime dt = cn.hutool.core.date.DateUtil.lastMonth();
            String s = dt.toDateStr();
            devHisElectricalDto.setBeginDate(s);
        }
        PageVo<EsDevHisWater> page = esDevHisWaterService.searchByCenterId(centerId,devHisElectricalDto.getUnitId() , devHisElectricalDto.getName(),(int) devHisElectricalDto.getPage(), (int) devHisElectricalDto.getLimit(),
                devHisElectricalDto.getBeginDate(),devHisElectricalDto.getEndDate());
        return CommonResult.success(page);
    }

    @ApiOperation(value = "火灾报警分析", notes = "火灾报警分析")
    @PostMapping("/fire/list")
    public CommonResult<PageVo<EsDevHis>> treeGridFire(@RequestBody DevHisElectricalDto devHisElectricalDto) {
        Integer enterPriseType = ShiroUtil.getUser().getEnterpriseType();
        String centerId ="";
        String enterpriseId = "";
        if (enterPriseType == 0){
            enterpriseId = ShiroUtil.getUser().getEnterpriseId().toString();
            devHisElectricalDto.setUnitId(enterpriseId);
        }else if(enterPriseType == 2){
            centerId = ShiroUtil.getUser().getEnterpriseId().toString();
        }else {
            return CommonResult.failed();
        }
        if (null == devHisElectricalDto.getEndDate()){
            devHisElectricalDto.setEndDate(DateUtil.getNowDateStr());
        }
        if (null == devHisElectricalDto.getBeginDate()) {
            DateTime dt = cn.hutool.core.date.DateUtil.lastMonth();
            String s = dt.toDateStr();
            devHisElectricalDto.setBeginDate(s);
        }
        PageVo<EsDevHis> page = esDevHisService.searchByCenterIdNew(centerId,devHisElectricalDto.getUnitId() , devHisElectricalDto.getName(),(int) devHisElectricalDto.getPage(), (int) devHisElectricalDto.getLimit(),
                devHisElectricalDto.getBeginDate(),devHisElectricalDto.getEndDate());
        return CommonResult.success(page);
    }

    @ApiOperation(value = "设备预警分析", notes = "设备预警分析")
    @GetMapping("/getEarlyWarning")
    public CommonResult<List<EarlyWarningResult>> getEarlyWarning(String beginTime,String endTime,Integer type,String unitId) {
        Integer enterPriseType = ShiroUtil.getUser().getEnterpriseType();
        String centerId ="";
        String enterpriseId = "";
        if (enterPriseType == 0){
            enterpriseId = ShiroUtil.getUser().getEnterpriseId().toString();
            unitId = enterpriseId;
        }else if(enterPriseType == 2){
            centerId = ShiroUtil.getUser().getEnterpriseId().toString();
        }else {
            return CommonResult.failed();
        }

        List<EarlyWarningResult> list = new ArrayList<>();
        if (type==1){
            list = ideviceAnalysisService.getEarlyWarningByYear(beginTime,endTime,unitId,centerId);
        }
        if (type==2){
            list = ideviceAnalysisService.getEarlyWarningByQuarter(beginTime,endTime,unitId,centerId);
        }
        if (type==3){
            list = ideviceAnalysisService.getEarlyWarningByMonth(beginTime,endTime,unitId,centerId);
        }
        if (type==4){
            list = ideviceAnalysisService.getEarlyWarningByDay(beginTime,endTime,unitId,centerId);
        }

        return CommonResult.success(list);
    }

    @ApiOperation(value = "设备预警当日总量", notes = "设备预警当日总量")
    @GetMapping("/getSumNumToday")
    public CommonResult<Integer> getSumNumToday() {
        Integer res = ideviceAnalysisService.getSumNumToday();
        return CommonResult.success(res);
    }

    @ApiOperation(value = "设备预警总量", notes = "设备预警总量")
    @GetMapping("/getSumNumAll")
    public CommonResult<Integer> getSumNumAll() {
        Integer res = ideviceAnalysisService.getSumNumAll();
        return CommonResult.success(res);
    }

    @ApiOperation(value = "单位，任意时间段，排名", notes = "单位，任意时间段，排名")
    @GetMapping("/getLevelByUnitIdAndTime")
    public CommonResult<List<DeviceRes>> getLevelByUnitIdAndTime(String moudleType,String unitIds,String beginTime,String endTime) {
        List<DeviceRes> list = ideviceAnalysisService.getLevelByUnitIdAndTime(moudleType,unitIds,beginTime,endTime);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "单位，任意时间段，走势", notes = "单位，任意时间段，走势")
    @GetMapping("/getTrendByUnitIdAndTime")
    public CommonResult<List<DeviceRes>> getTrendByUnitIdAndTime(String moudleType,String unitIds,String beginTime,String endTime,String timeGranularity) {
        List<DeviceRes> list = ideviceAnalysisService.getTrendByUnitIdAndTime(moudleType,unitIds,beginTime,endTime,timeGranularity);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "区域，行业，任意时间段，排名", notes = "区域，行业，任意时间段，排名")
    @GetMapping("/getAreaOrBusinessByTime")
    public CommonResult<List<DeviceRes>> getAreaOrBusinessByTime(String moudleType,String beginTime,String endTime,String dataDimension) {
        List<DeviceRes> list = ideviceAnalysisService.getAreaOrBusinessByTime(moudleType,beginTime,endTime,dataDimension);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "区域，行业，任意时间段，走势", notes = "区域，行业，任意时间段，走势")
    @GetMapping("/getAreaOrBusinessTrend")
    public CommonResult<List<DeviceRes>> getAreaOrBusinessTrend(String moudleType,String beginTime,String endTime,String dataDimension,String timeGranularity) {
        List<DeviceRes> list = ideviceAnalysisService.getAreaOrBusinessTrend(moudleType,beginTime,endTime,dataDimension,timeGranularity);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "区域，行业，任意时间段，占比", notes = "区域，行业，任意时间段，占比")
    @GetMapping("/getAreaOrBusinessRatio")
    public CommonResult<List<DeviceRes>> getAreaOrBusinessRatio(String moudleType,String beginTime,String endTime,String dataDimension) {
        List<DeviceRes> list = ideviceAnalysisService.getAreaOrBusinessRatio(moudleType,beginTime,endTime,dataDimension);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "同比 单位 总数对比 分单位总数对比", notes = "同比 单位 总数对比 分单位总数对比")
    @GetMapping("/getTongBiByTimeAndUnitId")
    public CommonResult<List<TongHuanBiRes>> getTongBiByTimeAndUnitId(String moudleType, String beginTime, String endTime, String timeGranularity, String unitIds) {
        List<TongHuanBiRes> list = ideviceAnalysisService.getTongBiByTimeAndUnitId(moudleType,beginTime,endTime,timeGranularity,unitIds);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "同比 单位 总数对比 分单位 分月 对比", notes = "同比 单位 总数对比 分单位 分月 对比")
    @GetMapping("/getTongBiByTimeAndUnitIdByMonth")
    public CommonResult<List<TongHuanBiRes>> getTongBiByTimeAndUnitIdByMonth(String moudleType, String beginTime, String endTime, String timeGranularity, String unitIds) {
        List<TongHuanBiRes> list = ideviceAnalysisService.getTongBiByTimeAndUnitIdByMonth(moudleType,beginTime,endTime,timeGranularity,unitIds);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "同比 区域 行业 分项总数对比", notes = "同比 区域 行业 分项总数对比")
    @GetMapping("/getAreaOrBusinessTongBiByTime")
    public CommonResult<List<TongHuanBiRes>> getAreaOrBusinessTongBiByTime(String moudleType, String beginTime, String endTime, String timeGranularity, String dataDimension) {
        List<TongHuanBiRes> list = ideviceAnalysisService.getAreaOrBusinessTongBiByTime(moudleType,beginTime,endTime,timeGranularity,dataDimension);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "同比 区域 行业 分项总数对比 分月", notes = "同比 区域 行业 分项总数对比 分月")
    @GetMapping("/getAreaOrBusinessTongBiByTimeByMonth")
    public CommonResult<List<TongHuanBiRes>> getAreaOrBusinessTongBiByTimeByMonth(String moudleType, String beginTime, String endTime, String timeGranularity, String dataDimension) {
        List<TongHuanBiRes> list = ideviceAnalysisService.getAreaOrBusinessTongBiByTimeByMonth(moudleType,beginTime,endTime,timeGranularity,dataDimension);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "环比 单位 总数对比 分单位总数对比", notes = "环比 单位 总数对比 分单位总数对比")
    @GetMapping("/getHuanBiByTimeAndUnitId")
    public CommonResult<List<TongHuanBiRes>> getHuanBiByTimeAndUnitId(String moudleType, String beginTime, String endTime, String timeGranularity, String unitIds) {
        List<TongHuanBiRes> list = ideviceAnalysisService.getHuanBiByTimeAndUnitId(moudleType,beginTime,endTime,timeGranularity,unitIds);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "环比 区域 行业 分项总数对比", notes = "环比 区域 行业 分项总数对比")
    @GetMapping("/getAreaOrBusinessHuanBiByTime")
    public CommonResult<List<TongHuanBiRes>> getAreaOrBusinessHuanBiByTime(String moudleType, String beginTime, String endTime, String timeGranularity, String dataDimension) {
        List<TongHuanBiRes> list = ideviceAnalysisService.getAreaOrBusinessHuanBiByTime(moudleType,beginTime,endTime,timeGranularity,dataDimension);
        return CommonResult.success(list);
    }
}
